package com.shanghaionstar.user;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.weight.ToastView;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.iview.IVehicleInfoView;
import com.shanghaionstar.user.presenter.VehicleInfoPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/25.
 */
public class UpdateInsuranceCompanyFragment extends BaseFragment implements IVehicleInfoView {
    @FindViewById
    private CustomLineLayout china_pingan_insurance_layout, china_renming_insurance_layout, china_taipingyang_insurance_layout, other_layout;
    @FindViewById
    private CommonInputLayout input_insurance;
    @FindViewById
    private CommonProgressButton btn_submit;
    @FindViewById
    private TopLayout top_layout;
    private VehicleInfoPresenter presenter;
    private String insuranceName = "";
    private BaseActivity parentActivity;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        btnBackClick(top_layout);
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.InsuranceCo_Back.getStringValue());
                back();
            }
        });
        parentActivity = (BaseActivity) getActivity();
        if (isFirstLoad()) {
            presenter = new VehicleInfoPresenter((parentActivity).getScheduler());
            presenter.setView(this);

            if (getArguments() == null) {
                return;
            }
            showRightArrow(getArguments().getString(FinalConstants.PAGE_JUMP));


            china_pingan_insurance_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showRightArrow(china_pingan_insurance_layout);
                }
            });
            china_renming_insurance_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showRightArrow(china_renming_insurance_layout);
                }
            });
            china_taipingyang_insurance_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showRightArrow(china_taipingyang_insurance_layout);
                }
            });
            other_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showRightArrow(other_layout);
                }
            });


            btn_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!china_pingan_insurance_layout.isShowRightArrow() && !china_renming_insurance_layout.isShowRightArrow() &&
                            !china_taipingyang_insurance_layout.isShowRightArrow() && !other_layout.isShowRightArrow()) {
                        return;
                    }
                    if (other_layout.isShowRightArrow()) {
                        if (CommonUtil.isNull(input_insurance.getText())) {
                            input_insurance.setCheckHintText(R.string.input_insurance_company);
                            return;
                        } else {
                            insuranceName = input_insurance.getText();
                        }
                    }

                    if (CommonUtil.isNull(insuranceName)) {
                        new ToastView(onActivity()).showToast(getString(R.string.select_insurance_company_hint));
                        return;
                    }

                    presenter.updateVehicleInfo(insuranceName, "", "", false);
                    presenter.start();
                }
            });
        }
    }

    private void showRightArrow(CustomLineLayout layout) {
        china_pingan_insurance_layout.setRightArrow(layout == china_pingan_insurance_layout ? R.drawable.iccon_selected : -1);
        china_renming_insurance_layout.setRightArrow(layout == china_renming_insurance_layout ? R.drawable.iccon_selected : -1);
        china_taipingyang_insurance_layout.setRightArrow(layout == china_taipingyang_insurance_layout ? R.drawable.iccon_selected : -1);
        other_layout.setRightArrow(layout == other_layout ? R.drawable.iccon_selected : -1);
        input_insurance.setVisibility(layout == other_layout ? View.VISIBLE : View.INVISIBLE);
        insuranceName = layout.getText();
    }

    private void showRightArrow(String selectValue) {
        if (TextUtils.isEmpty(selectValue)) {
            china_pingan_insurance_layout.setRightArrow(-1);
            china_renming_insurance_layout.setRightArrow(-1);
            china_taipingyang_insurance_layout.setRightArrow(-1);
            other_layout.setRightArrow(-1);
        } else {
            china_pingan_insurance_layout.setRightArrow(selectValue.equalsIgnoreCase(china_pingan_insurance_layout.getText()) ? R.drawable.iccon_selected : -1);
            china_renming_insurance_layout.setRightArrow(selectValue.equalsIgnoreCase(china_renming_insurance_layout.getText()) ? R.drawable.iccon_selected : -1);
            china_taipingyang_insurance_layout.setRightArrow(selectValue.equalsIgnoreCase(china_taipingyang_insurance_layout.getText()) ? R.drawable.iccon_selected : -1);
        }


        if (!CommonUtil.isNull(selectValue)) {
            other_layout.setRightArrow(!china_pingan_insurance_layout.isShowRightArrow()
                    && !china_renming_insurance_layout.isShowRightArrow()
                    && !china_taipingyang_insurance_layout.isShowRightArrow() ? R.drawable.iccon_selected : -1);
            input_insurance.setVisibility(View.VISIBLE);

            if (input_insurance.getVisibility() == View.VISIBLE) {
                input_insurance.setText(selectValue);
            }
        }
        insuranceName = selectValue;
    }

    @Override
    public int getLayoutId() {
        return R.layout.more_update_insurance_company;
    }

    @Override
    public void setLoading(boolean isLoading) {
        btn_submit.showLoading(isLoading);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
    }

    @Override
    public void onReponseSuccess(PlateEngineNumEntity object) {
        if (!CommonUtil.isNull(insuranceName)) {
            if (parentActivity instanceof MoreActivity) {
                ((MoreActivity) parentActivity).setInsuranceComp(insuranceName);
            }
        }
        new ToastView(onActivity()).showToast(getString(R.string.china_insurance_success));

   /*     VehicleInfoFragment fragment=new VehicleInfoFragment();

        Bundle bundle=new Bundle();
        bundle.putString("engineNumber",null);
        bundle.putString("plateNumber",null);
        bundle.putString("insuranceComp",((MoreActivity) getActivity()).getInsuranceComp());
        fragment.setArguments(bundle);
        ((MoreActivity) getActivity()).changeFragment(fragment, VehicleInfoFragment.class, false);*/
        PreferencesUtls.getInstance(parentActivity).putString("insuranceComp", insuranceName);
        back();
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}
